package com.liang.publics.ad.ads;

import android.app.Activity;
import android.content.Context;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.FrameLayout;

import androidx.fragment.app.DialogFragment;

import com.bytedance.sdk.openadsdk.mediation.manager.MediationFullScreenManager;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationRewardManager;
import com.liang.publics.config.Const;
import com.liang.maogouyuyin.R;


public  abstract   class AsDialogFragmentService extends DialogFragment {


    long millisInFuture = 12000;

    //---------------AD
    private FrameLayout mFeedContainer;
    //信息流
    private  String mFeedMediaId;
    //插屏
    private  String fullmMediaId;
    //激励
    private  String rewardmMediaId;



    public     int adCount=1;

    private static Activity activity;


    private MediationFeedService mediationFeedService;
    private MediationInterstitialFullService mediationInterstitialFullService;
    private MediationInterstitialFullService mediationInterstitialFullService2;
    public   MediationRewardService mediationRewardService;

    public void initAd(String type ,Context context,FrameLayout frameLayout){
        new Thread(new Runnable() {
            @Override
            public void run() {
//                Log.i(Const.TAG,"--------------------"+type);
                    if("feed".equals(type)) {
                        //feed
                        mFeedContainer = frameLayout;
                        mFeedMediaId = getResources().getString(R.string.feed_native_media_id);
                        mediationFeedService =  new MediationFeedService();
                        mediationFeedService.intiDate(context, mFeedContainer, mFeedMediaId);

                    }else if("full".equals(type)) {


                        // 插屏
                        fullmMediaId = getResources().getString(R.string.full_media_id);

                        mediationInterstitialFullService = new   MediationInterstitialFullService();
                        mediationInterstitialFullService.intiDate(context, fullmMediaId);

                    }else if("full2".equals(type)) {


                        // 插屏
                        fullmMediaId = getResources().getString(R.string.full_media_2_id);

                        mediationInterstitialFullService2 = new   MediationInterstitialFullService();
                        mediationInterstitialFullService2.intiDate(context, fullmMediaId);

                    }else if("reward".equals(type)) {

                        //激励视频
                        rewardmMediaId = getResources().getString(R.string.reward_media_id);
                        mediationRewardService = new MediationRewardService();
                        mediationRewardService.intiDate(context, rewardmMediaId);

                    }
                }
        }).start();
    }




    private  CountDownTimer timerFeed ;
    private  CountDownTimer timerFull  ;
    private  CountDownTimer timerFull2;
    private  CountDownTimer rewardFull ;
    public void showAd(String type,Activity  activitys){

        activity = activitys;

        if("feed".equals(type)) {//信息流
              timerFeed = new CountDownTimer(millisInFuture, 20) {

                @Override
                public void onTick(long l) {




                    if(mediationFeedService!=null ) {
                        Log.d(Const.TAG, "showAd---feed------------showFeedAdCount:" + mediationFeedService.showFeedAdCount);

                        if (  mediationFeedService.mTTFeedAd != null && mediationFeedService.showFeedAdCount < adCount)
                        {
                            timerFeed.cancel();
                            mediationFeedService.showFeedAd(activity);
                        }
                    }
                }

                @Override
                public void onFinish() {

                }
            };
            timerFeed.start();
        }else if("full".equals(type)) {//插屏

            long duration = millisInFuture+2000;
              timerFull = new CountDownTimer(duration, 20) {

                @Override
                public void onTick(long l) {
                    float num = (duration - l) / (float) duration * 100;
                    boolean ifShow = false;

                    if(mediationInterstitialFullService!=null ) {
                        if (mediationInterstitialFullService.ifShowInterstitialFullAd == false) {
                            if (mediationInterstitialFullService.mTTFullScreenVideoAd != null && mediationInterstitialFullService.showInterstitialFullAdCount < adCount) {
//
//
//                            if (MediationFeedService.ifShowFeedAd == true && "0".equals(MediationFeedService.ifFeedShow)) {//成功
//                                ifShow = true;
//                            } else if (MediationFeedService.ifShowFeedAd == true && "2".equals(MediationFeedService.ifFeedShow)) {//失败
//                                ifShow = true;
//                            } else if (num > 95) {
//                                ifShow = true;
//                            }
//                        } else if (num > 95) {
//                            ifShow = true;
//                        }

                                if (mediationInterstitialFullService.ifOnFullScreenVideoCached == true) {
                                    MediationFullScreenManager manager = mediationInterstitialFullService.getManager();
                                    if (manager != null && manager.isReady()) {
                                        ifShow = true;
                                    } else if (num > 95) {
                                        ifShow = true;
                                    }
                                } else if (num > 95) {
                                    ifShow = true;
                                }
                            }

                        }
                        Log.d(Const.TAG, "showAd---full---------ifShowInterstitialFullAd:" + mediationInterstitialFullService.ifShowInterstitialFullAd + "----showInterstitialFullAdCount:" + mediationInterstitialFullService.showInterstitialFullAdCount + "-------l:" + num + "---------ifShow:" + ifShow);

                    }
                    if(ifShow){
                          timerFull.cancel();
                        mediationInterstitialFullService.showInterstitialFullAd(activity);
                    }
                }

                @Override
                public void onFinish() {

                }
            };
            timerFull.start();
        }else if("full2".equals(type)) {//插屏
            long duration = millisInFuture+2000;
             timerFull2 = new CountDownTimer(duration, 20) {

                @Override
                public void onTick(long l) {
                    float num = (duration - l) / (float) duration * 100;
                    boolean ifShow = false;

                    if(mediationInterstitialFullService2!=null) {
                        if (mediationInterstitialFullService2.ifShowInterstitialFullAd == false) {
                            if (mediationInterstitialFullService2.mTTFullScreenVideoAd != null && mediationInterstitialFullService2.showInterstitialFullAdCount < adCount) {
//
//
//                            if (MediationFeedService.ifShowFeedAd == true && "0".equals(MediationFeedService.ifFeedShow)) {//成功
//                                ifShow = true;
//                            } else if (MediationFeedService.ifShowFeedAd == true && "2".equals(MediationFeedService.ifFeedShow)) {//失败
//                                ifShow = true;
//                            } else if (num > 95) {
//                                ifShow = true;
//                            }
//                        } else if (num > 95) {
//                            ifShow = true;
//                        }

                                if (mediationInterstitialFullService2.ifOnFullScreenVideoCached == true) {
                                    MediationFullScreenManager manager = mediationInterstitialFullService2.getManager();
                                    if (manager != null && manager.isReady()) {
                                        ifShow = true;
                                    } else if (num > 95) {
                                        ifShow = true;
                                    }
                                } else if (num > 95) {
                                    ifShow = true;
                                }
                            }

                        }
                        Log.d(Const.TAG,"showAd---full2---------ifShowInterstitialFullAd:"+mediationInterstitialFullService2.ifShowInterstitialFullAd+"----showInterstitialFullAdCount:"+mediationInterstitialFullService2.showInterstitialFullAdCount+"-------l:"+num+"---------ifShow:"+ifShow);

                    }

                    if(ifShow){
                        timerFull2.cancel();
                        mediationInterstitialFullService2.showInterstitialFullAd(activity);
                    }
                }

                @Override
                public void onFinish() {

                }
            };
            timerFull2.start();
        }else if("reward".equals(type)) {
            long duration = millisInFuture+2000;
              rewardFull = new CountDownTimer(millisInFuture, 20) {

                @Override
                public void onTick(long l) {
                    float num = (duration - l) / (float) duration * 100;
                    boolean ifShow = false;

                    if(mediationRewardService!=null ) {
                        if (mediationRewardService.ifShowRewardVideoAd == false) {
                            if (mediationRewardService.mTTRewardVideoAd != null && mediationRewardService.showRewardVideoAdCount < adCount) {

                                if (mediationRewardService.onRewardVideoCached == true) {
                                    MediationRewardManager manager = mediationRewardService.getManager();
                                    if (manager != null && manager.isReady()) {
                                        ifShow = true;
                                    } else if (num > 95) {
                                        ifShow = true;
                                    }
                                } else if (num > 95) {
                                    ifShow = true;
                                }


                            }
                        }
                        Log.d(Const.TAG, "showAd---reward---------ifShowRewardVideoAd:" + mediationRewardService.ifShowRewardVideoAd + "----showRewardVideoAdCount:" + mediationRewardService.showRewardVideoAdCount + "-------l:" + num + "---------ifShow:" + ifShow);

                    }
                    if(ifShow) {
                        rewardFull.cancel();
                        mediationRewardService.showRewardVideoAd(activity);
                    }
                }

                @Override
                public void onFinish() {

                }
            };
            rewardFull.start();
        }

    }




}
